package com.briup.jdk8.day02;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 常用的函数式接口
 * Predicate ，用于判断的条件
 */
public class Test1 {
    public static void main(String[] args) {
        // 使用过滤器，完成一些数组的过滤
        String[] strings = new String[]{"C","C++","C#","Java","JS"};
        Integer[] integers = new Integer[]{20,53,104,61};
        Double[] doubles = new Double[]{3.4,1.5,9.100};
        // 进行过滤
        List<String> l1 = intArrayFilter(strings, s -> s.length()>2);
        System.out.println("l1 = " + l1);
        List<Integer> l2 = intArrayFilter(integers, s -> s > 50);
        System.out.println("l2 = " + l2);
        // 在double的数组中，筛选出大于3小于1的元素
        Predicate<Double> p1 = s->s>3;
        Predicate<Double> p2 = s->s<1;
        List<Double> l3 = intArrayFilter(doubles, p1.or(p2).negate());
        System.out.println("l3 = " + l3);
        // 静态方法
        boolean abc = Predicate.isEqual(256).test(256);
        System.out.println("abc = " + abc);
    }
    public static <T> List<T> intArrayFilter(T[] arr, Predicate<T> p){
        // 准备一个list集合，将符合条件的放入进去
        List<T> list = new ArrayList<>();
        for (T t : arr){
            if(p.test(t)){
                list.add(t);
            }
        }
        return list;
    }
}
